<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>roundedbox.cpp 示例文件 | Qt Widgets 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="qtwidgets-graphicsview-boxes-example.html">Boxes</a></td><td >roundedbox.cpp 示例文件</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">roundedbox.cpp 示例文件</h1>
<span class="subtitle">graphicsview/boxes/roundedbox.cpp</span>
<!-- $$$graphicsview/boxes/roundedbox.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">

  <span class="comment">/****************************************************************************
  **
  ** Copyright (C) 2016 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the demonstration applications of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:BSD$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** BSD License Usage
  ** Alternatively, you may use this file under the terms of the BSD license
  ** as follows:
  **
  ** &quot;Redistribution and use in source and binary forms, with or without
  ** modification, are permitted provided that the following conditions are
  ** met:
  **   * Redistributions of source code must retain the above copyright
  **     notice, this list of conditions and the following disclaimer.
  **   * Redistributions in binary form must reproduce the above copyright
  **     notice, this list of conditions and the following disclaimer in
  **     the documentation and/or other materials provided with the
  **     distribution.
  **   * Neither the name of The Qt Company Ltd nor the names of its
  **     contributors may be used to endorse or promote products derived
  **     from this software without specific prior written permission.
  **
  **
  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/</span>

  <span class="preprocessor">#include &quot;roundedbox.h&quot;</span>

  <span class="comment">//============================================================================//</span>
  <span class="comment">//                                P3T2N3Vertex                                //</span>
  <span class="comment">//============================================================================//</span>

  VertexDescription P3T2N3Vertex<span class="operator">::</span>description<span class="operator">[</span><span class="operator">]</span> <span class="operator">=</span> {
      {VertexDescription<span class="operator">::</span>Position<span class="operator">,</span> GL_FLOAT<span class="operator">,</span> SIZE_OF_MEMBER(P3T2N3Vertex<span class="operator">,</span> position) <span class="operator">/</span> <span class="keyword">sizeof</span>(<span class="type">float</span>)<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span>}<span class="operator">,</span>
      {VertexDescription<span class="operator">::</span>TexCoord<span class="operator">,</span> GL_FLOAT<span class="operator">,</span> SIZE_OF_MEMBER(P3T2N3Vertex<span class="operator">,</span> texCoord) <span class="operator">/</span> <span class="keyword">sizeof</span>(<span class="type">float</span>)<span class="operator">,</span> <span class="keyword">sizeof</span>(QVector3D)<span class="operator">,</span> <span class="number">0</span>}<span class="operator">,</span>
      {VertexDescription<span class="operator">::</span>Normal<span class="operator">,</span> GL_FLOAT<span class="operator">,</span> SIZE_OF_MEMBER(P3T2N3Vertex<span class="operator">,</span> normal) <span class="operator">/</span> <span class="keyword">sizeof</span>(<span class="type">float</span>)<span class="operator">,</span> <span class="keyword">sizeof</span>(QVector3D) <span class="operator">+</span> <span class="keyword">sizeof</span>(QVector2D)<span class="operator">,</span> <span class="number">0</span>}<span class="operator">,</span>

      {VertexDescription<span class="operator">::</span>Null<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span>}<span class="operator">,</span>
  };

  <span class="comment">//============================================================================//</span>
  <span class="comment">//                                GLRoundedBox                                //</span>
  <span class="comment">//============================================================================//</span>

  <span class="type">float</span> lerp(<span class="type">float</span> a<span class="operator">,</span> <span class="type">float</span> b<span class="operator">,</span> <span class="type">float</span> t)
  {
      <span class="keyword">return</span> a <span class="operator">*</span> (<span class="number">1.0f</span> <span class="operator">-</span> t) <span class="operator">+</span> b <span class="operator">*</span> t;
  }

  GLRoundedBox<span class="operator">::</span>GLRoundedBox(<span class="type">float</span> r<span class="operator">,</span> <span class="type">float</span> scale<span class="operator">,</span> <span class="type">int</span> n)
      : GLTriangleMesh<span class="operator">&lt;</span>P3T2N3Vertex<span class="operator">,</span> <span class="type">unsigned</span> <span class="type">short</span><span class="operator">&gt;</span>((n<span class="operator">+</span><span class="number">2</span>)<span class="operator">*</span>(n<span class="operator">+</span><span class="number">3</span>)<span class="operator">*</span><span class="number">4</span><span class="operator">,</span> (n<span class="operator">+</span><span class="number">1</span>)<span class="operator">*</span>(n<span class="operator">+</span><span class="number">1</span>)<span class="operator">*</span><span class="number">24</span><span class="operator">+</span><span class="number">36</span><span class="operator">+</span><span class="number">72</span><span class="operator">*</span>(n<span class="operator">+</span><span class="number">1</span>))
  {
      <span class="type">int</span> vidx <span class="operator">=</span> <span class="number">0</span><span class="operator">,</span> iidx <span class="operator">=</span> <span class="number">0</span>;
      <span class="type">int</span> vertexCountPerCorner <span class="operator">=</span> (n <span class="operator">+</span> <span class="number">2</span>) <span class="operator">*</span> (n <span class="operator">+</span> <span class="number">3</span>) <span class="operator">/</span> <span class="number">2</span>;

      P3T2N3Vertex <span class="operator">*</span>vp <span class="operator">=</span> m_vb<span class="operator">.</span>lock();
      <span class="type">unsigned</span> <span class="type">short</span> <span class="operator">*</span>ip <span class="operator">=</span> m_ib<span class="operator">.</span>lock();

      <span class="keyword">if</span> (<span class="operator">!</span>vp <span class="operator">|</span><span class="operator">|</span> <span class="operator">!</span>ip) {
          <a href="../qtcore/qtglobal.html#qWarning">qWarning</a>(<span class="string">&quot;GLRoundedBox::GLRoundedBox: Failed to lock vertex buffer and/or index buffer.&quot;</span>);
          m_ib<span class="operator">.</span>unlock();
          m_vb<span class="operator">.</span>unlock();
          <span class="keyword">return</span>;
      }

      <span class="keyword">for</span> (<span class="type">int</span> corner <span class="operator">=</span> <span class="number">0</span>; corner <span class="operator">&lt;</span> <span class="number">8</span>; <span class="operator">+</span><span class="operator">+</span>corner) {
          QVector3D centre(corner <span class="operator">&amp;</span> <span class="number">1</span> <span class="operator">?</span> <span class="number">1.0f</span> : <span class="operator">-</span><span class="number">1.0f</span><span class="operator">,</span>
                  corner <span class="operator">&amp;</span> <span class="number">2</span> <span class="operator">?</span> <span class="number">1.0f</span> : <span class="operator">-</span><span class="number">1.0f</span><span class="operator">,</span>
                  corner <span class="operator">&amp;</span> <span class="number">4</span> <span class="operator">?</span> <span class="number">1.0f</span> : <span class="operator">-</span><span class="number">1.0f</span>);
          <span class="type">int</span> winding <span class="operator">=</span> (corner <span class="operator">&amp;</span> <span class="number">1</span>) <span class="operator">^</span> ((corner <span class="operator">&gt;</span><span class="operator">&gt;</span> <span class="number">1</span>) <span class="operator">&amp;</span> <span class="number">1</span>) <span class="operator">^</span> (corner <span class="operator">&gt;</span><span class="operator">&gt;</span> <span class="number">2</span>);
          <span class="type">int</span> offsX <span class="operator">=</span> ((corner <span class="operator">^</span> <span class="number">1</span>) <span class="operator">-</span> corner) <span class="operator">*</span> vertexCountPerCorner;
          <span class="type">int</span> offsY <span class="operator">=</span> ((corner <span class="operator">^</span> <span class="number">2</span>) <span class="operator">-</span> corner) <span class="operator">*</span> vertexCountPerCorner;
          <span class="type">int</span> offsZ <span class="operator">=</span> ((corner <span class="operator">^</span> <span class="number">4</span>) <span class="operator">-</span> corner) <span class="operator">*</span> vertexCountPerCorner;

          <span class="comment">// Face polygons</span>
          <span class="keyword">if</span> (winding) {
              ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx;
              ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx <span class="operator">+</span> offsX;
              ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx <span class="operator">+</span> offsY;

              ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx <span class="operator">+</span> vertexCountPerCorner <span class="operator">-</span> n <span class="operator">-</span> <span class="number">2</span>;
              ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx <span class="operator">+</span> vertexCountPerCorner <span class="operator">-</span> n <span class="operator">-</span> <span class="number">2</span> <span class="operator">+</span> offsY;
              ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx <span class="operator">+</span> vertexCountPerCorner <span class="operator">-</span> n <span class="operator">-</span> <span class="number">2</span> <span class="operator">+</span> offsZ;

              ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx <span class="operator">+</span> vertexCountPerCorner <span class="operator">-</span> <span class="number">1</span>;
              ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx <span class="operator">+</span> vertexCountPerCorner <span class="operator">-</span> <span class="number">1</span> <span class="operator">+</span> offsZ;
              ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx <span class="operator">+</span> vertexCountPerCorner <span class="operator">-</span> <span class="number">1</span> <span class="operator">+</span> offsX;
          }

          <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> n <span class="operator">+</span> <span class="number">2</span>; <span class="operator">+</span><span class="operator">+</span>i) {

              <span class="comment">// Edge polygons</span>
              <span class="keyword">if</span> (winding <span class="operator">&amp;</span><span class="operator">&amp;</span> i <span class="operator">&lt;</span> n <span class="operator">+</span> <span class="number">1</span>) {
                  ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx <span class="operator">+</span> i <span class="operator">+</span> <span class="number">1</span>;
                  ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx;
                  ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx <span class="operator">+</span> offsY <span class="operator">+</span> i <span class="operator">+</span> <span class="number">1</span>;
                  ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx <span class="operator">+</span> offsY;
                  ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx <span class="operator">+</span> offsY <span class="operator">+</span> i <span class="operator">+</span> <span class="number">1</span>;
                  ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx;

                  ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx <span class="operator">+</span> i;
                  ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx <span class="operator">+</span> <span class="number">2</span> <span class="operator">*</span> i <span class="operator">+</span> <span class="number">2</span>;
                  ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx <span class="operator">+</span> i <span class="operator">+</span> offsX;
                  ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx <span class="operator">+</span> <span class="number">2</span> <span class="operator">*</span> i <span class="operator">+</span> offsX <span class="operator">+</span> <span class="number">2</span>;
                  ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx <span class="operator">+</span> i <span class="operator">+</span> offsX;
                  ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx <span class="operator">+</span> <span class="number">2</span> <span class="operator">*</span> i <span class="operator">+</span> <span class="number">2</span>;

                  ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> (corner <span class="operator">+</span> <span class="number">1</span>) <span class="operator">*</span> vertexCountPerCorner <span class="operator">-</span> <span class="number">1</span> <span class="operator">-</span> i;
                  ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> (corner <span class="operator">+</span> <span class="number">1</span>) <span class="operator">*</span> vertexCountPerCorner <span class="operator">-</span> <span class="number">2</span> <span class="operator">-</span> i;
                  ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> (corner <span class="operator">+</span> <span class="number">1</span>) <span class="operator">*</span> vertexCountPerCorner <span class="operator">-</span> <span class="number">1</span> <span class="operator">-</span> i <span class="operator">+</span> offsZ;
                  ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> (corner <span class="operator">+</span> <span class="number">1</span>) <span class="operator">*</span> vertexCountPerCorner <span class="operator">-</span> <span class="number">2</span> <span class="operator">-</span> i <span class="operator">+</span> offsZ;
                  ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> (corner <span class="operator">+</span> <span class="number">1</span>) <span class="operator">*</span> vertexCountPerCorner <span class="operator">-</span> <span class="number">1</span> <span class="operator">-</span> i <span class="operator">+</span> offsZ;
                  ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> (corner <span class="operator">+</span> <span class="number">1</span>) <span class="operator">*</span> vertexCountPerCorner <span class="operator">-</span> <span class="number">2</span> <span class="operator">-</span> i;
              }

              <span class="keyword">for</span> (<span class="type">int</span> j <span class="operator">=</span> <span class="number">0</span>; j <span class="operator">&lt;</span><span class="operator">=</span> i; <span class="operator">+</span><span class="operator">+</span>j) {
                  QVector3D normal <span class="operator">=</span> QVector3D(i <span class="operator">-</span> j<span class="operator">,</span> j<span class="operator">,</span> n <span class="operator">+</span> <span class="number">1</span> <span class="operator">-</span> i)<span class="operator">.</span>normalized();
                  QVector3D offset(<span class="number">0.5f</span> <span class="operator">-</span> r<span class="operator">,</span> <span class="number">0.5f</span> <span class="operator">-</span> r<span class="operator">,</span> <span class="number">0.5f</span> <span class="operator">-</span> r);
                  QVector3D pos <span class="operator">=</span> centre <span class="operator">*</span> (offset <span class="operator">+</span> r <span class="operator">*</span> normal);

                  vp<span class="operator">[</span>vidx<span class="operator">]</span><span class="operator">.</span>position <span class="operator">=</span> scale <span class="operator">*</span> pos;
                  vp<span class="operator">[</span>vidx<span class="operator">]</span><span class="operator">.</span>normal <span class="operator">=</span> centre <span class="operator">*</span> normal;
                  vp<span class="operator">[</span>vidx<span class="operator">]</span><span class="operator">.</span>texCoord <span class="operator">=</span> QVector2D(pos<span class="operator">.</span>x() <span class="operator">+</span> <span class="number">0.5f</span><span class="operator">,</span> pos<span class="operator">.</span>y() <span class="operator">+</span> <span class="number">0.5f</span>);

                  <span class="comment">// Corner polygons</span>
                  <span class="keyword">if</span> (i <span class="operator">&lt;</span> n <span class="operator">+</span> <span class="number">1</span>) {
                      ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx;
                      ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx <span class="operator">+</span> i <span class="operator">+</span> <span class="number">2</span> <span class="operator">-</span> winding;
                      ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx <span class="operator">+</span> i <span class="operator">+</span> <span class="number">1</span> <span class="operator">+</span> winding;
                  }
                  <span class="keyword">if</span> (i <span class="operator">&lt;</span> n) {
                      ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx <span class="operator">+</span> i <span class="operator">+</span> <span class="number">1</span> <span class="operator">+</span> winding;
                      ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx <span class="operator">+</span> i <span class="operator">+</span> <span class="number">2</span> <span class="operator">-</span> winding;
                      ip<span class="operator">[</span>iidx<span class="operator">+</span><span class="operator">+</span><span class="operator">]</span> <span class="operator">=</span> vidx <span class="operator">+</span> <span class="number">2</span> <span class="operator">*</span> i <span class="operator">+</span> <span class="number">4</span>;
                  }

                  <span class="operator">+</span><span class="operator">+</span>vidx;
              }
          }

      }

      m_ib<span class="operator">.</span>unlock();
      m_vb<span class="operator">.</span>unlock();
  }

</pre>
</div>
<!-- @@@graphicsview/boxes/roundedbox.cpp -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
